using System.Collections.Generic;
using Dream.Dialogue.Implement;
using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;

namespace Dream.ScenesLoader
{
    //sky 24-11-25 15:37:20 
    public class DefultController : MonoBehaviour
    {
        [SerializeField] private List<string> talkListSleep;
        [SerializeField] private List<string> talkListAwake;
        private bool _continueTalk;
        private int _currentLine;
        private bool _isAwake;
        private bool _isAwakeAnim;
        private bool _isContinue;

        private bool _isInput;
        private bool _isNow;
        private bool _isSleepAnim;
        private bool _isTalk;
        private InteractTalk interactTalk;

        private GameObject talkBox;

        private void Awake()
        {
            Init();
        }

        private void Start()
        {
            // 对话框出现前的动画播放，之后将isTalk设置为true
            AnimBeforeTalk();
        }

        private void Update()
        {
            if (_isInput)
                _continueTalk = LightInputManager.Instance.ContinueTalkInput;
            BeforeEnterDream();
        }

        private void OnDestroy()
        {
            EventManager.Unregister<IsAwake>(IsAwakeInit);
        }

        private void Init()
        {
            talkBox = transform.Find("TalkBox").gameObject;
            interactTalk = talkBox.GetComponentInChildren<InteractTalk>();
            talkBox.SetActive(false);
            _currentLine = 0;
            EventManager.Register<IsAwake>(IsAwakeInit);
            _isContinue = true;
            _isInput = true;
            _isNow = false;
            _continueTalk = false;
            _isSleepAnim = true;
            _isAwakeAnim = false;
            _isAwake = false;
        }

        public void IsAwakeInit(IsAwake evt)
        {
            if (!evt.isSleepOrAwake)
            {
                _isSleepAnim = false;
                _isAwakeAnim = true;
                _isAwake = true;
                _isTalk = true;
            }
        }

        private void BeforeEnterDream()
        {
            //播放完动准备进入梦境
            if (!_isTalk)
                return;
            if ((_currentLine >= talkListSleep.Count && !_isAwake) ||
                (_currentLine >= talkListAwake.Count && _isAwake))
                _isContinue = false;
            //睡觉前播放的对话
            if (_continueTalk && _isContinue && !_isAwake)
            {
                talkBox.SetActive(true);
                DisplayNextLine(talkListSleep[_currentLine]);
            }

            //醒来播放放的对话
            if (_continueTalk && _isContinue && _isAwake)
            {
                talkBox.SetActive(true);
                DisplayNextLine(talkListAwake[_currentLine]);
            }

            //对话播放完准备进入梦境
            if (_continueTalk && !_isContinue && !_isAwake)
            {
                talkBox.SetActive(false);
                EnterDream();
            }

            //醒来播放完对话准备进入下一天
            if (_continueTalk && !_isContinue && _isAwake)
            {
                talkBox.SetActive(false);
                EnterNextDay();
            }
        }

        private void DisplayNextLine(string story)
        {
            interactTalk.SetWord(story);
            _isNow = interactTalk.StartTalk();
            if (!_isNow)
                ++_currentLine;
        }

        private void EnterDream()
        {
            _currentLine = 0;
            _isContinue = true;
            _isAwake = true;
            using var evt = NextSceneLoad.Get();
            evt.isFadeScreen = true;
            evt.isExitScene = false;
            EventManager.SendEvent(evt);
            _isInput = false;
            _continueTalk = false;
        }

        private void AnimBeforeTalk()
        {
            _isTalk = false;
            //播放动画
            if (_isSleepAnim)
            {
                //动画处理
            }

            //设置isTalk为true
            _isTalk = true;
        }

        private void EnterNextDay()
        {
            _currentLine = 0;
            _isAwake = false;
            _isContinue = true;
            //进行进入下一天的相关操作
            if (_isAwakeAnim)
            {
                //过度处理

                _isSleepAnim = true;
                _isAwakeAnim = false;
            }

            //播方睡觉前动画
            AnimBeforeTalk();
        }
    }
}